package com.automannn.websoketmqtt.provider.rest;

import com.automannn.websoketmqtt.common.amqp.DirectGlobalValue;
import com.automannn.websoketmqtt.common.amqp.FanoutGlobalValue;
import com.automannn.websoketmqtt.common.amqp.TopicGlobalValue;
import com.automannn.websoketmqtt.common.message.WsMessage;
import com.automannn.websoketmqtt.common.message.WsMessageQueueProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.webservices.WebServicesProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author chenkh
 * @time 2020/10/12 16:51
 */
@RestController
public class SendMessageController {
    @Autowired
   private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage(){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "hello,this is the first rabbit demo by automannn.";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map = new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);

        rabbitTemplate.convertAndSend(DirectGlobalValue.EXCHANGE_NAME, DirectGlobalValue.ROUTING_KEY,map);
        return "ok";
    }


    @GetMapping("/sendTopicMessage1")
    public String sendTopicMessage1(){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "messageData:MAN";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map = new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);

        rabbitTemplate.convertAndSend(TopicGlobalValue.TOPIC_EXCHANGE_NAME, TopicGlobalValue.TOPIC_FLAG+".man",map);
        return "ok";
    }

    @GetMapping("/sendTopicMessage2")
    public String sendTopicMessage2(){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "messageData:WOMAN is all";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map = new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);

        rabbitTemplate.convertAndSend(TopicGlobalValue.TOPIC_EXCHANGE_NAME, TopicGlobalValue.TOPIC_FLAG+".woman",map);
        return "ok";
    }

    @GetMapping("/sendFanoutMessage")
    public String sendFanoutMessage(){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "messageData:this is fanout message";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map = new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);

        rabbitTemplate.convertAndSend(FanoutGlobalValue.FANOUT_EXCHANGE_NAME, null,map);
        return "ok";
    }


    @GetMapping("/sendWsMessage")
    public String sendWsMessage(){

        rabbitTemplate.convertAndSend(WsMessageQueueProperties.WS_EXCHANGE, "ws.a","aaa");
        return "ok";
    }
}
